package org.midy.nio;

import org.midy.infrastructure.tools.Logger;

import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;

public class SocketChannelTestMain {
    public static void main(String[] args) {
        try {
            SocketChannel client = SocketChannel.open(new InetSocketAddress("127.0.0.1", 8090));
            client.configureBlocking(false);
            Logger.info("client.isOpen() = %b", client.isOpen());

            while ( !client.isConnected() && client.isConnectionPending() ) {
                Logger.info(" is connectionPending ");
                Thread.sleep(1000);
            }

            if( !client.isConnected() ) {
                throw new RuntimeException(" 连接失败");
            }

            Logger.info("client.isOpen() = %b", client.isOpen());
            client.shutdownInput();
            Logger.info("shutdownInput client.isOpen() = %b", client.isOpen());
            client.shutdownOutput();
            Logger.info("shutdownOutput client.isOpen() = %b", client.isOpen());
            client.close();
            Logger.info("close client.isOpen() = %b", client.isOpen());
            Logger.info("client.isConnected() = %b", client.isConnected());
            client.close();
        } catch ( Exception e) {
            e.printStackTrace();
        }
    }
}
